using System;

namespace Pdf2epub.Model
{
    public class FontUsage : IComparable
    {
        public FontUsage()
        {
        }

        public FontUsage(string pdfFontName, int fontSize)
        {
            FontId = GetFontId(pdfFontName);
            FontName = GetFontName(pdfFontName);
            FontSize = fontSize;
            UsageCount = 1;
        }

        public string FontId { get; set; }

        public string FontName { get; set; }

        public int FontSize { get; set; }

        public int UsageCount { get; set; }

        public static string GetFontId(string pdfFontName)
        {
            int idx = pdfFontName.IndexOf('+');
            if (idx != -1)
                return pdfFontName.Substring(0, idx);
            return pdfFontName;
        }

        public static string GetFontName(string pdfFontName)
        {
            int idx = pdfFontName.IndexOf('+') + 1;
            return pdfFontName.Substring(idx);
        }

        public int CompareTo(object obj)
        {
            if (obj is FontUsage)
            {
                return ToString().CompareTo(obj.ToString());
            }
            return -99;
        }

        public override string ToString()
        {
            return string.Format("{0} ({1})", FontName, FontSize);
        }
    }
}